﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameControl;
using QxFramework.Core;
using System;
using GameMap;
public class RaySwordFunc : ItemFunc
{
    /// <summary>
    /// 激光宽度
    /// </summary>
    public float RayWidth = 0.25f;
    /// <summary>
    /// 当前生成的激光的名字
    /// </summary>
    string RayName;
    /// <summary>
    /// 生成的激光对应的物体
    /// </summary>
   // RayItem Ray;
    /// <summary>
    /// 用来设置激光形态
    /// </summary>
    Transform anchor1, anchor2;
    public Color RayColor;
    [Header("当敌人不攻击时,光剑将在此时间后消失")]
    public float ExistTimeWhenUseByEnmey = 1f;
    private void OnEnable()
    {
        //找到锚点
        anchor1 = this.transform.Find("Anchor1");
        anchor2 = this.transform.Find("Anchor2");
    }
    void InitPos(RayItemField ray)
    {
        //根据生成物名字获取生成物实例
        //设置激光位置
        ray.Position = this.transform.TransformPoint((anchor1.localPosition + anchor2.localPosition) / 2);
        ray.RayColor = new Vector3(RayColor.r,RayColor.g,RayColor.b);
        //获取激光长度
        Vector3 vtr = anchor2.localPosition - anchor1.localPosition;
        //设置激光形态
        ray.TriggerColliderPoints=RayItem.GetColliderPointsByBounds(new  Bounds(Vector3.zero, new Vector3(Mathf.Abs(vtr.x), RayWidth, 0)));
        vtr = anchor2.position - anchor1.position;
        //设置激光角度
        ray.Rotation = Mathf.Atan2(vtr.y, vtr.x) / Mathf.PI * 180;
        //  Ray.transform.parent = this.transform;
    }
    public override void KeyDownInvoke()
    {
        base.KeyDownInvoke();
        //生成一个激光
        MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.CreateInMap, this,new GameMap.ItemCreateArgs("Ray", ThisItem.ItemFieldSetting.Position,(ItemField field) =>
        {
            //强制转型后获取id
            RayName = field.ItemName;
            //添加伤害忽视
            GameManager.Get<IItemHealthManager>().IgnorePairs.UniqueAdd(new Tuple<string, string>(RayName, ThisItem.ItemFieldSetting.ItemName));
            //更新生成物体的位置
            InitPos(field as RayItemField);
        }));
    }
    void UpdateRayPos()
    {
        if (ClockManager.Get<IItemSyncManager>().GetGameObjectByName(RayName) == null)
        {
            return;
        }
        //根据生成物名字获取生成物实例
        var Ray = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(RayName).GetComponent<RayItem>();
        //设置激光位置
        Ray.transform.position = this.transform.TransformPoint((anchor1.localPosition + anchor2.localPosition) / 2);
        Ray.RayColor = RayColor;
        //获取激光长度
        Vector3 vtr = anchor2.localPosition - anchor1.localPosition;
        //设置激光形态
        Ray.RayBounds = new Bounds(Vector3.zero, new Vector3(Mathf.Abs(vtr.x), RayWidth,0));
        vtr = anchor2.position - anchor1.position;
        //设置激光角度
        Ray.RotationInDegree =Mathf.Atan2(vtr.y, vtr.x)/Mathf.PI*180;
      //  Ray.transform.parent = this.transform;
    }
    public override void KeyOnInvoke()
    {
        //当保持按键触发时,实时更新激光位置
        base.KeyOnInvoke();
        UpdateRayPos();
    }
    public virtual void FixedUpdate()
    {
        if (isInvokeByEnemyTimecnt >= 0)
        {
            isInvokeByEnemyTimecnt --;
            Debug.Log($"cnt={isInvokeByEnemyTimecnt}");
            if (isInvokeByEnemyTimecnt < 0)
            {
                KeyUpInvoke();
                isInvokeByEnemyTimecnt = -1;
            }
            else
            {
                KeyOnInvoke();
            }
        }
    }
    public override void KeyUpInvoke()
    {
        base.KeyUpInvoke();
        if (RayName != default)
        {
            //当抬起按键,销毁激光
            MessageManager.Instance.Get<ItemMapEvent>().DispatchMessage(ItemMapEvent.DeleteInMap, this, new ItemMapArgs { ItemName =RayName},null);
        }
        RayName = default;
    }
    public override void OnDestory()
    {
        base.OnDestory();
        if (RayName != default)
        {
            //当自身销毁,连带销毁子物体
            MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.DeleteInMap, this, new GameMap.ItemMapArgs { ItemName = RayName }, null);
        }
        RayName = default;
    }
    int isInvokeByEnemyTimecnt=-1;
    public override void InvokeByEnmey()
    {
        base.InvokeByEnmey();
        Debug.Log("Invoke");
        if (isInvokeByEnemyTimecnt < 0)
        {

            KeyDownInvoke();
        }
        isInvokeByEnemyTimecnt =Mathf.RoundToInt( ExistTimeWhenUseByEnmey/Time.fixedDeltaTime);
    }
}
